# 使用 PHP 官方镜像作为基础镜像
FROM php:latest

# 设置环境变量，避免交互式安装提示
ENV DEBIAN_FRONTEND=noninteractive

# 如果 /etc/apt/sources.list 文件不存在，则创建它，并替换为阿里云源
RUN if [ ! -f /etc/apt/sources.list ]; then \
        echo "deb http://mirrors.aliyun.com/debian bullseye main contrib non-free" > /etc/apt/sources.list && \
        echo "deb-src http://mirrors.aliyun.com/debian bullseye main contrib non-free" >> /etc/apt/sources.list && \
        echo "deb http://mirrors.aliyun.com/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list && \
        echo "deb-src http://mirrors.aliyun.com/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list && \
        echo "deb http://mirrors.aliyun.com/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list && \
        echo "deb-src http://mirrors.aliyun.com/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list; \
    fi

# 强制更新 apt 并设置缓存选项，加快访问速度
RUN apt-get clean && apt-get update && \
    apt-get install -y --no-install-recommends \
    software-properties-common \
    gnupg2 \
    ca-certificates \
    lsb-release \
    apt-transport-https

RUN touch /etc/apt/sources.list.d/php-ondrej.list && \
    echo 'deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main' > /etc/apt/sources.list.d/php-ondrej.list && \
    echo 'deb-src http://ppa.launchpad.net/ondrej/php/ubuntu focal main' >> /etc/apt/sources.list.d/php-ondrej.list && \
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E5267A6C && \
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 71DAEAAB4AD4CAB6

# 安装系统依赖和 PHP 扩展
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libxml2-dev \
    libevent-dev \
    openssl \
    libssl-dev \
    pkg-config \
    libcurl4-openssl-dev \
    git \
    unzip &&\
    docker-php-ext-configure gd --with-freetype --with-jpeg && \
    docker-php-ext-install gd pdo pdo_mysql pcntl sockets && \
    docker-php-ext-enable gd pdo pdo_mysql pcntl sockets && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*


# 有时候下载会超时，所以把这个文件下载到本地安装，具体可查看gitee地址
COPY ./redis-6.0.2.tgz /tmp/redis-6.0.2.tgz
COPY ./event-3.1.4.tgz /tmp/event-3.1.4.tgz
RUN pecl install /tmp/redis-6.0.2.tgz && pecl install /tmp/event-3.1.4.tgz && docker-php-ext-enable redis

# 设置工作目录
WORKDIR /var/www

# 暴露端口
EXPOSE 8787

# 启动命令
CMD ["php", "/var/www/start.php", "start"]
